package com.pfzy.lib.sdk.log;

import android.content.Context;

import com.getkeepsafe.relinker.ReLinker;
import com.tencent.mars.xlog.Log;
import com.tencent.mars.xlog.Xlog;

public class UXLog {
    private static long MAX_XLOG_FILE_SIZE = 10485760;
    private static String mLogPath = null;
    private static String mNamePrefix = null;
    private static Context mContext = null;
    private static long mMaxLogDirSizeMb = 0;
    private static int mLogMode = 0;
    private static boolean mXlogInitOk = false;
    protected static boolean mDebug = false;

    public static void reInit() {
        init(mContext, mNamePrefix, mDebug, mLogMode, mMaxLogDirSizeMb);
    }

    public static void init(Context context, String str, boolean z) {
        init(context, str, z, 1);
    }

    public static void init(Context context, String namePrefix, boolean debug, int mode) {
        init(context, namePrefix, debug, mode, (UXLogDirSizeChecker.MAX_LOG_DIR_SIZE / 1024) / 1024);
    }

    public static void init(Context context, String namePrefix, boolean debug, int mode, long maxLogDirSizeMb) {
        String cachePath;
        mMaxLogDirSizeMb = maxLogDirSizeMb;
        mContext = context;
        mDebug = debug;
        mNamePrefix = namePrefix;
        switchLogConfig(mode);
        if (mLogMode == 1) {
            ReLinker.loadLibrary(context, "c++_shared");
            ReLinker.loadLibrary(context, "marsxlog");
            if (debug) {
                cachePath = context.getExternalFilesDir(null).getAbsolutePath() + "/xlog";
                mLogPath = context.getExternalFilesDir(null).getAbsolutePath() + "/log";
            } else {
                cachePath = context.getFilesDir().getAbsolutePath() + "/xlog";
                mLogPath = context.getFilesDir().getAbsolutePath() + "/log";
            }
            Xlog xlog = new Xlog();
            xlog.setMaxFileSize(0L, MAX_XLOG_FILE_SIZE);
            xlog.setMaxAliveTime(0L, 432000L);
            Log.setLogImp(xlog);
            if (debug) {
                Log.setConsoleLogOpen(true);
                Log.appenderOpen(1, 0, cachePath, mLogPath, namePrefix, 0);
            } else {
                Log.setConsoleLogOpen(false);
                Log.appenderOpen(2, 0, cachePath, mLogPath, namePrefix, 0);
            }
            if (maxLogDirSizeMb >= 20 && maxLogDirSizeMb <= 100) {
                UXLogDirSizeChecker.MAX_LOG_DIR_SIZE = maxLogDirSizeMb * 1024 * 1024;
            }
            UXLogDirSizeChecker.checkLogFolderSize(context, getLogPath());
            mXlogInitOk = true;
        } else {
            Log.setLogImp(new AndroidLog());
            mXlogInitOk = false;
        }
        dumpSysInfo();
    }

    private static void switchLogConfig(int mode) {
        if (mode == 1) {
            mLogMode = 1;
        } else if (mode != 2) {
        } else {
            mLogMode = 2;
        }
    }

    public static boolean isXlogInitOk() {
        return mLogMode == 1 && mXlogInitOk;
    }

    public static void appenderClose() {
        if (isXlogInitOk()) {
            Log.appenderClose();
        }
    }

    public static void appenderFlush(boolean z) {
        if (isXlogInitOk()) {
            Log.appenderFlushSync(z);
        }
    }

    public static String getLogPath() {
        return mLogPath;
    }

    public static String getNamePrefix() {
        return mNamePrefix;
    }

    public static void d(String tag, String msg) {
        Log.d(tag, msg);
    }

    private static void dumpSysInfo() {
        i("DEVICE_INFO", Log.getSysInfo());
    }

    public static void e(String tag, String msg) {
        Log.e(tag, msg);
    }

    public static void e(String tag, String msg, Throwable th) {
        Log.e(tag, msg + '\n' + android.util.Log.getStackTraceString(th));
    }

    public static void i(String tag, String msg) {
        Log.i(tag, msg);
    }

    public static void i(String tag, String msg, Throwable th) {
        Log.i(tag, msg + '\n' + android.util.Log.getStackTraceString(th));
    }

    public static void v(String tag, String msg) {
        Log.v(tag, msg);
    }

    public static void w(String tag, String msg) {
        Log.w(tag, msg);
    }

    public static void w(String tag, String msg, Throwable th) {
        Log.w(tag, msg + '\n' + android.util.Log.getStackTraceString(th));
    }
}
